<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/29
 * Time: 13:50
 * 微信支付页面
 */
namespace wechatpay;
use Endroid\QrCode\QrCode;

//require 'includes/loadwx.php';
//require 'endroid/qr-code/src/QrCode.php';
//require 'includes/OrderClass.php';
use wechatpay\includes\OrderClass;
//判断AJax请求
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))
{
    $p = $_REQUEST['p'];
    $WxPay = OrderClass::load_wechat("pay");
    //生成订单
    $order_sn = date('YmdHis') . rand(100, 10000);
    //支付成功回调地址
    $url = $_SERVER['HTTP_HOST']  . '/notify.php';
    $body = '用户充值';
    //二维码有效时间
    $end_time = 7200;
    //$sign 用于接收微信返回信息
    $sign = '';
    $amount = $p * 100;
    $code_url = $WxPay->getPrepayId(null, $body, $order_sn, $amount, $url, 'NATIVE', $sign);
    $md5_sign = isset($sign['sign']) ? $sign['sign'] : '';
    if (isset($sign["sign"]))
        unset($sign['sign']);
    //手动签名
    $new_sign = \Wechat\Lib\Tools::getPaySign($sign, $WxPay->partnerKey);
    if ($md5_sign != $new_sign || $code_url === false) {
       exit('创建订单失败，请重试');
    }

    $QrCode = new  QrCode($code_url);
    $img = $QrCode->getDataUri();
  //生成订单
    $data = [
        'order_sn' => $order_sn,
        'order_title' => $body,
        'amount' => $amount / 100,
        'create_time' => time(),
        'over_time' => $end_time,
        'pay_status' => 0,
        'code_url' => $code_url,
        'pay_type' => 0
    ];
    $object = new OrderClass();
    $object->filed = ObjectFromArray($object->filed,$data);
    //插入数据
        $rs = $object->save();

    if ($rs===false) {
        exit('创建订单失败，请重试');
    }
    //获取新数据ID
    $data['order_id'] = $object->filed->id;

}





?>
<style>
    #pay-body {
        width: 310px;
        margin: auto;
    }

    .pay-header {
        text-align: left;
        position: relative;
        padding: 0 55px;
        margin: 10px 0;
    }

    .pay-header img {
        width: 66px;
        height: 66px;
        display: inline-block;
    }

    .pay-header span {
        display: inline-block;
        padding: 0 20px;
        margin-top: 7px;
        position: absolute;
    }

    .pay-header a {
        display: block;
    }

    .pay-header a.title_cn {
        font-size: 22px;
        font-weight: bold;
    }

    .pay-header a.title_en {
        font-size: 20px;
    }

    .pay-order {
        text-align: center;
        padding: 15px 15px;
        background-color: #2FCD38;
        min-height: 300px;
    }

    .pay-order p {
        color: #fff;
    }

    .pay-order img {
        height: 226px;
        width: 256px;
        padding: 10px;
        background-color: #fff;
        border-radius: 3px;
    }
</style>
<div id="pay-body" align="center">
    <input type="hidden" value="<?=$data['order_id']?>">
    <div class="pay-header">
        <img src="/static/img/wechat-pay.svg"><span><a class="title_cn">微信支付</a><a class="title_en">Wechat Pay</a></span>
    </div>
    <div class="pay-order">
        <p style="padding-bottom: 10px;">订单编号：<?=$data['order_sn']?></p>
        <img src="<?=$img?>">
        <p style="padding-top: 10px;">打开微信扫一扫二维码付款</p>
    </div>
</div>
<script>
    $('#pay-content').css('padding', '0');
    function clear_msg() {
        var pay = $('#pay-body');
        if (pay.length > 0) {
            var id = pay.children('input').val();
            $.ajax({
                type: 'post',
                url: 'clearpay.php',
                data: 'id=' + id,
                success: function (msg) {
                    if (msg == 'SUCCESS') {
                        clearInterval(window.pay_times);
                        window.pay_times = null;
                        loadData('/jump.php?id=' + id, '#pay-content');
                    }
                }
            })
        } else {
            clearInterval(window.pay_times);
            window.pay_times = null;
        }
    }
    if (!window.pay_times) {
        window.pay_times = setInterval('clear_msg()', 3000);
    }
</script>










